/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygang.helper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.exolab.castor.mapping.GeneralizedFieldHandler;

/**
 * This class represents Castor's date field handler. 
 * Needs to parse date which represented in the special format.
 * 
 * @author eugene
 */
public class CastorDateHandler extends GeneralizedFieldHandler {

    private SimpleDateFormat formatter = new SimpleDateFormat(Consts.NEWS_DATE_FORMAT);
    
    @Override
    public Object convertUponGet(Object o) {
        return o == null ? "" : formatter.format((Date)o);
    }

    @Override
    public Object convertUponSet(Object o) {
        try {
            return formatter.parse((String)o);
        } catch (ParseException ex) {
            Logger.getLogger(CastorDateHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return new Date();
    }

    @Override
    public Class getFieldType() {
        return Date.class;
    }
    
}
